package com.datong.server.strategy.view;

import com.datong.server.strategy.ContextStrategy;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

//策略模式主类
public class MainWindow {
    private static final MainWindow mainWindow = new MainWindow();
    private final HashMap<Integer,Integer> indexOfDiscount = new HashMap<Integer,Integer>(7) {{put(0,0);}{put(1,9);}{put(2,8);}{put(3,7);}{put(4,6);}{put(5,5);}{put(6,4);};};
    //单例模式-任意时刻只有一个窗口
    public static MainWindow getInstance(){
        return mainWindow;
    }
    private final String[] arrayCombo = {"---请选择---", "9折","8折","7折","6折","5折","4折"};
    private JLabel label_数量 = new JLabel();
    private JTextField textField_数量 = new JTextField();
    private JLabel label_单价 = new JLabel();
    private JTextField textField_单价 = new JTextField();
    private JButton button_确定 = new JButton("確定");
    private JButton button_重置 = new JButton("取消");
    private JLabel label_总额 = new JLabel();
    private JLabel label_折扣 = new JLabel("折扣：");
    private JComboBox comboBox_折扣 = new JComboBox(arrayCombo);

    private JLabel label_返现 = new JLabel();
    private JTextField jTextField_满 = new JTextField();
    private JLabel jLabel_返 = new JLabel();
    private JTextField textField_返 = new JTextField();

    public void startWindow(){
        JFrame jFrame = new JFrame();
        jFrame.setLayout(new FlowLayout());
        jFrame.setSize(250,550);
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);

        JPanel jPanel_单价 = getUnitPicePanel();
        jFrame.add(jPanel_单价);

        JPanel jPanel_数量 = getNumberPanel();
        jFrame.add(jPanel_数量);

        JPanel jPanel_折扣 = getDiscountPanel();
        jFrame.add(jPanel_折扣);

        JPanel jPanel_返现 = getReturnCash();
        jFrame.add(jPanel_返现);

        JPanel panel_按钮 = getBtnPanel();
        jFrame.add(panel_按钮);
        btnClickHandle();

        JPanel panel_总额 = getTotalAmountPanel();
        jFrame.add(panel_总额);
    }
    private void btnClickHandle(){
        button_确定.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str_unit = textField_单价.getText();
                String str_number = textField_数量.getText();
                if(str_number.isEmpty() || str_number.isEmpty()){
                    System.out.println("数量和单价都不能为空.");
                    return;
                }
                double unit = Float.valueOf(str_unit);
                double number = Float.valueOf(str_number);
                double discount = indexOfDiscount.get(comboBox_折扣.getSelectedIndex());//折扣
                String condition_str = jTextField_满.getText();
                String retCash_str = textField_返.getText();
                double condition = Double.valueOf(condition_str.isEmpty()?"0":condition_str);
                double retCash = Double.valueOf(retCash_str.isEmpty()?"0": retCash_str);
                Map<String,Double> settleType = new HashMap<>();
                settleType.put("unit",unit);
                settleType.put("number",number);
                settleType.put("discount",discount);
                settleType.put("retCashSign",0.00);
                if(condition!=0 && retCash != 0) {
                    settleType.put("retCashSign",1.00);
                    settleType.put("condition",condition);
                    settleType.put("retCash",retCash);
                }
                ContextStrategy contextStrategy = new ContextStrategy(settleType);
                double totalPrice = contextStrategy.ContextInterface();
                label_总额.setText("总额：" + totalPrice);
                StringBuilder stringBuilder = new StringBuilder("单价：" + unit + ", 数量：" + number + "，折扣：" + discount);
                if(condition!=0 && retCash != 0){
                    stringBuilder.append("，活动：满"+condition_str+" 返 " + retCash_str);
                }
                stringBuilder.append(", 总金额：" + totalPrice);
                System.out.println(stringBuilder);
            }
        });
        button_重置.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField_单价.setText("");
                textField_数量.setText("");
                label_总额.setText("总额：0");
            }
        });
    }
    private JPanel getReturnCash(){
        label_返现.setText("返现：满");
        jLabel_返.setText("返");
        jTextField_满.setPreferredSize(new Dimension(50,30));
        textField_返.setPreferredSize(new Dimension(50,30));
        JPanel jPanel = new JPanel();
        jPanel.setVisible(true);
        jPanel.setLayout(new FlowLayout());
        jPanel.add(label_返现);
        jPanel.add(jTextField_满);
        jPanel.add(jLabel_返);
        jPanel.add(textField_返);
        return jPanel;
    }
    private JPanel getDiscountPanel(){
        comboBox_折扣.setPreferredSize(new Dimension(150,30));
        label_折扣.setText("折扣：");
        JPanel jPanel = new JPanel();
        jPanel.setVisible(true);
        jPanel.setLayout(new FlowLayout());
        jPanel.add(label_折扣);
        jPanel.add(comboBox_折扣);
        return jPanel;
    }
    //获得单价Panel
    private JPanel getUnitPicePanel(){

        label_单价.setText("单价：");
        textField_单价.setPreferredSize(new Dimension(150,30));

        JPanel jPanel_单价 = new JPanel();
        jPanel_单价.setVisible(true);
        jPanel_单价.setLayout(new FlowLayout());
        jPanel_单价.add(label_单价);
        jPanel_单价.add(textField_单价);
        return jPanel_单价;
    }
    //获得数量Panel
    private JPanel getNumberPanel(){

        label_数量.setText("数量：");
        textField_数量.setPreferredSize(new Dimension(150,30));

        JPanel jPanel_数量 = new JPanel();
        jPanel_数量.setVisible(true);
        jPanel_数量.setLayout(new FlowLayout());
        jPanel_数量.add(label_数量);
        jPanel_数量.add(textField_数量);
        return jPanel_数量;
    }
    private JPanel getBtnPanel(){
        JPanel jPanel = new JPanel();
        jPanel.setVisible(true);
        jPanel.setLayout(new FlowLayout());
        jPanel.add(button_确定);
        jPanel.add(button_重置);
        return jPanel;
    }
    private JPanel getTotalAmountPanel(){
        label_总额.setText("总额：0.00");
        label_总额.setPreferredSize(new Dimension(150,50));

        JPanel jPanel_总额 = new JPanel();
        jPanel_总额.setLayout(new FlowLayout());
        jPanel_总额.setVisible(true);
        jPanel_总额.add(label_总额);
        return jPanel_总额;
    }
}
